package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.domain.medical.Data;
import com.gxuwz.medical.tools.StringUtil;

/**
 * 医疗机构管理控制处理
 * @author 沉羽墨玖
 *
 */
public class MedicalServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String  m=request.getParameter("m");//动作类型参数
		if("list".equals(m)){
			process(request, response, "/page/medical/medical_list.jsp");
		}else if("input".equals(m)){
			process(request, response, "/page/medical/medical_add.jsp");
		}else if("get".equals(m)){
			process(request, response, "/page/medical/medical_edit.jsp");
		}else if("add".equals(m)){
			add(request, response);
		}else if("edit".equals(m)){
			edit(request, response);
		}else if("del".equals(m)){
			del(request, response);
		}else{
			error(request,response);
		}
	}
	/**
	 * 添加医疗机构
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		 //接收页面传递过来的参数
		String instCode=request.getParameter("instCode");
		System.out.println("instCode==="+instCode);
		String orgaCode=request.getParameter("orgaCode");
		String instName=request.getParameter("instName");
		String regCode=request.getParameter("regCode");
		String areaCode=request.getParameter("areaCode");
		String strsubjection=request.getParameter("subjection");
		String strinstGrade=request.getParameter("instGrade");
		String strsbdd=request.getParameter("sbdd");
		String strapprove=request.getParameter("approve");
		String streconomy=request.getParameter("economy");
		String strhygieneBig=request.getParameter("hygieneBig");
		String strhygieneSmall=request.getParameter("hygieneSmall");
		String unit=request.getParameter("unit");
		String openDate=request.getParameter("openDate");//日期
		String represent=request.getParameter("represent");
		String fundsNum=request.getParameter("funds");
		double funds=Double.parseDouble(fundsNum);

		Data subjection=new Data(strsubjection, "", "02");
		Data instGrade=new Data(strinstGrade, "", "06");
		Data sbdd=new Data(strsbdd, "", "04");
		Data approve=new Data(strapprove, "", "04");
		Data economy=new Data(streconomy, "", "01");
		Data hygieneBig=new Data(strhygieneBig, "", "03");
		Data hygieneSmall=new Data(strhygieneSmall, "", "0301");
		instCode=StringUtil.trim(instCode);
		 Medical medical=new Medical(instCode, orgaCode, instName, regCode, areaCode, subjection, instGrade, sbdd, approve, economy, hygieneBig, hygieneSmall, unit, openDate, represent, funds);
		 //调用添加方法
		 try{
			 //Medical medical=new Medical(instCode, orgaCode, instName, regCode, areaCode, subjection, instGrade, sbdd, approve, economy, hygieneBig, hygieneSmall, unit, openDate, represent, funds);
			 medical.add();
			 process(request, response, "/page/medical/medical_list.jsp");
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 
	}
	/**
	 * 修改慢性病信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void edit(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

		//接收页面传递过来的参数
	  	  String instCode=request.getParameter("instCode");
	  	  String orgaCode=request.getParameter("orgaCode");
	  	String instName=request.getParameter("instName");
	  	String regCode=request.getParameter("regCode");
	  	String areaCode=request.getParameter("areaCode");
	  	String strsubjection=request.getParameter("subjection");
	  	  String strinstGrade=request.getParameter("instGrade");
	  	String strsbdd=request.getParameter("sbdd");
	  	String strapprove=request.getParameter("approve");
	  	String streconomy=request.getParameter("economy");
	  	String strhygieneBig=request.getParameter("hygieneBig");
	  	String strhygieneSmall=request.getParameter("hygieneSmall");
	    String unit=request.getParameter("unit");
	  	String openDate=request.getParameter("openDate");
	  	String represent=request.getParameter("represent");
	  	String fundsNum=request.getParameter("funds");
	  	double funds=Double.parseDouble(fundsNum);
	  	
	  	Data subjection=new Data(strsubjection, "", "02");
		Data instGrade=new Data(strinstGrade, "", "06");
		Data sbdd=new Data(strsbdd, "", "04");
		Data approve=new Data(strapprove, "", "04");
		Data economy=new Data(streconomy, "", "01");
	    Data hygieneBig=new Data(strhygieneBig, "", "03");
		Data hygieneSmall=new Data(strhygieneSmall, "", "0301");
		instCode=StringUtil.trim(instCode);
	  	  //2:构造新慢病信息对象
	  	  Medical medical=new Medical(instCode, orgaCode,instName,regCode,areaCode,subjection,instGrade,sbdd,approve,economy,hygieneBig,hygieneSmall,unit,openDate,represent,funds);  
	  	//3：调用保存的方法
	  	  try{
	  		medical.edit();
	  		process(request, response, "/page/medical/medical_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
		
	  
		}
	/**
	 * 删除医疗机构信息
	 * @throws ServletException
	 * @throws IOException
	 */
	private void del(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

	  	  //1：接收参数
	  	  String instCode=request.getParameter("instCode");
	  	  
	  	  //2:构造新慢病信息对象
	  	  Medical medical=new Medical();
	  	  try{
	  		System.out.println(instCode);
	  		medical.del(instCode);
	  		process(request, response, "/page/medical/medical_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
	  
		}

}

